package javaPersistence;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import beans.ProductosBean;

public class Producto {
	
	public boolean insertService(String nameService, double priceService, String descriptionService){
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("INSERT INTO SERVICIO (nombreServicio, precioServicio, descripcionServicio) VALUES (");
			query.append("'").append(nameService).append("',").append(priceService).append(",'").append(descriptionService).append("')");
			System.out.println("query: "+query.toString());
			if(dbConSoft.getDeclaracion().executeUpdate(query.toString()) > 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateService(int idService, String nameService, double priceService, String descriptionService){
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("UPDATE SERVICIO SET nombreServicio = '").append(nameService).append("', precioServicio = ").append(priceService).append(",descripcionServicio = '").append(descriptionService).append("' WHERE idServicio = ").append(idService);
			System.out.println("query: "+query.toString());
			if(dbConSoft.getDeclaracion().executeUpdate(query.toString()) > 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public ArrayList<ProductosBean> loadAllServices(){
		ArrayList<ProductosBean> arrService = new ArrayList<ProductosBean>();
		try {
			ProductosBean serviceB;
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder();
			query.append("SELECT * FROM SERVICIO");
			ResultSet resultSet = dbConSoft.getDeclaracion().executeQuery(query.toString());
			while (resultSet.next()) {
				serviceB = new ProductosBean();
				serviceB.setIdServicio(resultSet.getInt("idServicio"));
				serviceB.setNombreServicio(resultSet.getString("nombreServicio"));
				serviceB.setPrecioServicio(resultSet.getDouble("precioServicio"));
				serviceB.setDescripcionServicio(resultSet.getString("descripcionServicio"));
				arrService.add(serviceB);
			}
		} catch (SQLException e) {
		}
		return arrService;
	}
	
	public String executeDDL(String sqlDDL){
		String msg = "true";
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder(sqlDDL);
			System.out.println("query: "+query.toString());
			dbConSoft.getDeclaracion().execute(query.toString());
		} catch (SQLException e) {
			e.printStackTrace();
			msg = e.getMessage();
		} 
		return msg;
	}

}
